package com.my.oschina.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.my.oschina.R;
import com.my.oschina.app.AppContext;
import com.my.oschina.app.AppException;
import com.my.oschina.bean.Tweet;
import com.my.oschina.common.StringUtils;
import com.my.oschina.common.UIHelper;

/**
 * 动弹-详情
 */
public class TweetDetailTweetFragment extends SherlockFragment {

	private Layout layout;
	private Handler mHandler;
	private Tweet tweetDetail;

	/**
	 * 返回TweetDetailTweetFragment对象
	 * 
	 * @param curId
	 * @return ff TweetDetailTweetFragment对象
	 */
	public static TweetDetailTweetFragment newInstance(int curId) {
		TweetDetailTweetFragment ff = new TweetDetailTweetFragment();
		Bundle bundle = new Bundle();
		bundle.putInt("curId", curId);
		ff.setArguments(bundle);
		return ff;
	}

	public int getArgsCurId() {
		return getArguments().getInt("curId");
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		// outState.putSerializable("bean", bean);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.tweet_detail_tweet, container,
				false);
		layout = new Layout();
		layout.face = (ImageView) view.findViewById(R.id.tweet_detail_face);
		layout.name = (TextView) view.findViewById(R.id.tweet_detail_name);
		layout.time = (TextView) view.findViewById(R.id.tweet_detail_time);
		layout.content = (WebView) view.findViewById(R.id.tweet_detail_content);
		layout.image = (ImageView) view.findViewById(R.id.tweet_detail_image);
		layout.face.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		// new GetUserInfoTask().execute();
		initData();
	}

	/**
	 * 初始化控件数据-加载动弹
	 */
	private void initData() {

		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {

				if (msg.what == 1) {
					layout.name.setText(tweetDetail.getAuthor());
					// layout.name.setOnClickListener(faceClickListener);
					layout.time.setText(StringUtils.friendly_time(tweetDetail
							.getPubDate()));

					String body = UIHelper.WEB_STYLE + tweetDetail.getBody();
					body = body.replaceAll(
							"(<img[^>]*?)\\s+width\\s*=\\s*\\S+", "$1");
					body = body.replaceAll(
							"(<img[^>]*?)\\s+height\\s*=\\s*\\S+", "$1");

					layout.content.loadDataWithBaseURL(null, body, "text/html",
							"utf-8", null);
					layout.content
							.setWebViewClient(UIHelper.getWebViewClient());

					// 加载用户头像
					String faceURL = tweetDetail.getFace();
					if (faceURL.endsWith("portrait.gif")
							|| StringUtils.isEmpty(faceURL)) {
						layout.face.setImageResource(R.drawable.widget_dface);
					} else {
						UIHelper.showUserFace(layout.face, faceURL);
					}
					// layout.face.setOnClickListener(faceClickListener);

					// 加载图片
					String imgSmall = tweetDetail.getImgSmall();
					if (!StringUtils.isEmpty(imgSmall)) {
						UIHelper.showLoadImage(layout.image, imgSmall, null);
						layout.image.setVisibility(View.VISIBLE);
						// image.setOnClickListener(imageClickListener);
					}

					// 发送通知广播
					if (msg.obj != null) {
						// UIHelper.sendBroadCast(getSherlockActivity(),
						// (Notice) msg.obj);
					}
				} else if (msg.what == 0) {
					UIHelper.ToastMessage(getSherlockActivity(),
							R.string.msg_load_is_null);
				} else {
					((AppException) msg.obj).makeToast(getSherlockActivity());
				}
			}
		};

		loadTweetDetail(getArgsCurId(), mHandler, true);
	}

	/**
	 * 线程加载动弹详情
	 * 
	 * @param tweetId
	 * @param handler
	 */
	private void loadTweetDetail(final int tweetId, final Handler handler,
			final boolean isRefresh) {

		new Thread() {
			@Override
			public void run() {
				Message msg = new Message();
				try {
					tweetDetail = ((AppContext) getSherlockActivity()
							.getApplication()).getTweet(tweetId, isRefresh);
					msg.what = (tweetDetail != null && tweetDetail.getId() > 0) ? 1
							: 0;
					msg.obj = (tweetDetail != null) ? tweetDetail.getNotice()
							: null;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}

	private class Layout {
		ImageView face;
		TextView name;
		TextView time;
		WebView content;
		ImageView image;
	}

}
